<template>
  <div>
    <Breadcrumb :opt-data="protocolTypeOptData"></Breadcrumb>
    <PageLayout>
      <div class="table-container height-100">
        <EZTable
          v-loading="tableLoading"
          :table-id="tableId"
          :table-hdl="tableHdl"
          :rows="protocolTypeData"
          :custom-empty="true"
          @pageChanged="getProtocolTypeData"
          @sizeChanged="getProtocolTypeData"
          @filterChange="getProtocolTypeData">
          <EmptyContent slot="empty" :opt-data="protocolTypeOptData"></EmptyContent>
        </EZTable>
      </div>
      <el-dialog
        :title="`${!isEditProtocolType ? '新增' : '编辑'}协议类型`"
        :visible.sync="showProtocolTypeDialog"
        :close-on-click-modal="false"
        width="600px"
        center>
        <el-form
          v-loading="protocolTypeFormLoading"
          :ref="protocolTypeFormRef"
          :model="protocolTypeForm"
          :rules="protocolTypeFormRules"
          size="small"
          label-width="130px">
          <el-form-item label="协议类型名" prop="name">
            <el-input v-model="protocolTypeForm.name" placeholder="请填写协议类型"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="protocolTypeForm.remarks" placeholder="请填写备注"></el-input>
          </el-form-item>
          <div class="text-align-right">
            <el-form-item>
              <el-button size="medium" @click="protocolTypeFormCanceled">取消</el-button>
              <el-button size="medium" type="primary" @click="protocolTypeFormSubmitted">提交</el-button>
            </el-form-item>
          </div>
        </el-form>
      </el-dialog>
    </PageLayout>
  </div>
</template>

<script>
  import {commonConfig} from "@/config/common";
  import {tableIdConfig, tableOptBtnConfig} from '@/config/table';
  import {msgCode} from "@/config/string";
  import {confirmHandle, deepCopyWithJson, messageHandle} from "@/utils";
  import {assembleTableOptBtn, getTableConfig, TableStateHdl} from "@/modules/routeParamStateHdl";
  import protocolTypeTableCfg from "@/config/table/protocolType";
  import EZTable from '@/components/table/EZTable';

  export default {
    name: "ProtocolType",
    components: {
      EZTable
    },
    data() {
      return {
        commonConfig: commonConfig,
        tableLoading: true,
        tableId: tableIdConfig.protocolType,
        tableHdl: null,
        protocolTypeData: [],
        showProtocolTypeDialog: false,
        protocolTypeFormLoading: false,
        protocolTypeForm: {},
        protocolTypeFormRules: {
          name: [
            {required: true, message: '请填写协议类型', trigger: 'blur'}
          ],
        },
        protocolTypeFormRef: 'protocolTypeFormRef',
        isEditProtocolType: false
      }
    },
    computed: {
      protocolTypeOptData() {
        return [
          {
            type: 'btn',
            label: '新增协议类型',
            icon: 'icon-tianjia',
            callback: () => {
              this.clickCreateBtn();
            }
          }
        ];
      }
    },
    activated() {
      this.initTableConfig();
      this.initProtocolTypeForm();
      this.getProtocolTypeData();
    },
    methods: {
      initTableConfig() {
        if (this.tableHdl) return;
        const {columns = [], filters = []} = getTableConfig(this.tableId, protocolTypeTableCfg);
        assembleTableOptBtn(columns, ({type = '', data = {}}) => {
          if (!type) return;
          const protocolType = deepCopyWithJson(data);
          switch (type) {
            case tableOptBtnConfig.edit.type:
              this.showProtocolTypeDialog = true;
              this.isEditProtocolType = true;
              this.protocolTypeForm = protocolType;
              break;
            case tableOptBtnConfig.delete.type:
              this.deleteProtocolType(protocolType);
              break;
            default:
              break;
          }
        });
        this.tableHdl = new TableStateHdl(this.$route.name, this.tableId, columns, filters);
      },
      initProtocolTypeForm() {
        this.protocolTypeForm = {
          name: '',
          remarks: '',
        }
      },
      resetProtocolTypeForm() {
        this.initProtocolTypeForm();
        const protocolTypeForm = this.$refs[this.protocolTypeFormRef];
        protocolTypeForm && protocolTypeForm.resetFields();
      },
      clickCreateBtn() {
        this.isEditProtocolType && this.resetProtocolTypeForm();
        this.showProtocolTypeDialog = true;
        this.isEditProtocolType = false;
      },
      getProtocolTypeData() {
        this.tableLoading = true;
        const selector = {};
        const tableParam = this.tableHdl.assembleTableParam();
        API.protocolType.protocolTypeList({selector, ...tableParam}).then(result => {
          this.tableLoading = false;
          const {data = [], total = 0} = result;
          this.protocolTypeData = data;
          this.tableHdl.total = total;
        }).catch(err => {
          console.log('err', err)
          this.tableLoading = false;
        });
      },
      deleteProtocolType(protocolType = {}) {
        confirmHandle({label: `确定要删除协议类型：${protocolType.name} 吗?`}).then(() => {
          API.protocolType.protocolTypeDelete({_id: protocolType._id}).then(result => {
            messageHandle({code: msgCode.DELETE_SUCCESS, title: '协议类型'});
            this.tableHdl.judgeTableMatched();
            this.getProtocolTypeData();
            this.resetProtocolTypeForm();
          });
        }).catch(() => {
        });
      },
      protocolTypeFormCanceled() {
        this.showProtocolTypeDialog = false;
      },
      protocolTypeFormSubmitted() {
        this.$refs[this.protocolTypeFormRef].validate((valid) => {
          if (!valid) return;
          this.protocolTypeFormLoading = true;
          if (!this.isEditProtocolType) {
            API.protocolType.protocolTypeAdd({...this.protocolTypeForm}).then(result => {
              messageHandle({code: msgCode.ADD_SUCCESS, title: '协议类型'});
              this.showProtocolTypeDialog = false;
              this.protocolTypeFormLoading = false;
              this.getProtocolTypeData();
              this.resetProtocolTypeForm();
            }).catch(err => {
              console.log('err', err)
              this.protocolTypeFormLoading = false;
            });
          } else {
            API.protocolType.protocolTypeEdit({...this.protocolTypeForm}).then(result => {
              messageHandle({code: msgCode.EDIT_SUCCESS, title: '协议类型'});
              this.showProtocolTypeDialog = false;
              this.protocolTypeFormLoading = false;
              this.getProtocolTypeData();
              this.resetProtocolTypeForm();
            }).catch(err => {
              console.log('err', err)
              this.protocolTypeFormLoading = false;
            });
          }
        });
      },
    },
  }
</script>

<style scoped>

</style>
